package com.oraking.android.hybridapp.dev.korealaw.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

public class CmmHttpUtil {
	
	public static String sendHttpPost(String address,  ArrayList<HashMap<String, String>> list){
		URL url;
		
		HashMap<String, String> map;
		
		Set<String> set;
		Iterator<String> it;
		
		StringBuffer sb = null;
		String key;
		
		if(list != null && list.size() >0){
			try {
				url = new URL(address);
				
				HttpURLConnection http = (HttpURLConnection)url.openConnection();
				
				http.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
				http.setConnectTimeout(5000);
				http.setRequestMethod("POST");
				http.setDoInput(true);
				http.setDoOutput(true);
				
				sb = new StringBuffer();
				
				for(int i = 0; i < list.size(); i++){
					sb.append("&");
					map = list.get(i);
					set = map.keySet();
					it = set.iterator();
					while(it.hasNext()){
						key = (String)it.next();
						
						sb.append(key);
						sb.append("=");
						sb.append(map.get(key).toString());
					}
				}
				
				PrintWriter pw = new PrintWriter(new OutputStreamWriter(http.getOutputStream(), "UTF-8"));
				pw.write(sb.toString());
				pw.flush();
				
				BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream(), "UTF-8"), 10240);
				
				sb = new StringBuffer();
				String inputLine;
				while((inputLine = in.readLine())!=null){
					sb.append(inputLine);
				}
				
				pw.close();
				in.close();
			} catch (Exception e) {
				//Log.e(CmmMobLawStr.ERROR, e.getMessage());
			}
		}
		
		return sb.toString();
	}
	
	public static String fromHtmlToStr(String documentURL){
		StringBuffer url_content = new StringBuffer();
		
		try{
			URL url = new URL(documentURL);
			InputStream is = url.openStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);

			String inStr;
			while((inStr = br.readLine())!=null){
				url_content.append(inStr+"\n");
			}
		}catch(Exception e){
			Log.e("error", e.getMessage());
		}
		
		Pattern p = Pattern.compile(CmmStr.REG_EXP_CHECKBOX);
		Matcher m = p.matcher(url_content.toString());
		url_content = new StringBuffer(m.replaceAll(""));
		
		p = Pattern.compile(CmmStr.REG_EXP_IMG);
		m = p.matcher(url_content.toString());
		url_content = new StringBuffer(m.replaceAll(""));
		
		p = Pattern.compile(CmmStr.REG_EXP_P);
		m = p.matcher(url_content.toString());
		url_content = new StringBuffer(m.replaceAll(CmmStr.CHG_EXP_P));
		
		p = Pattern.compile(CmmStr.REG_EXP_A);
		m = p.matcher(url_content.toString());
		url_content = new StringBuffer(m.replaceAll(""));
		
		return url_content.toString();
	}
	
	public static ArrayList<HashMap<String, String>> makeURL(String searchText, String target, int page){
    	ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
    	HashMap<String, String> hm;
    	
    	// Search Target
    	if("".equals(target) || CmmStr.LAW.equals(target)){
    		hm = new HashMap<String, String>();
    		hm.put(CmmStr.TARGET, CmmStr.LAW);
    		list.add(hm);
    	}
    	
    	// Search Keyword
    	if(searchText != null && !"".equals(searchText)){
    		hm = new HashMap<String, String>();
    		try {
				hm.put(CmmStr.QUERY, URLEncoder.encode(searchText,"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				Log.e(CmmStr.ERROR, "ERROR : CmmHttpUtil makeURL");
			}
    		list.add(hm);
    	}
    	
    	// Page Setting
    	hm = new HashMap<String, String>();
    	hm.put(CmmStr.PAGE, String.valueOf(page));
    	list.add(hm);
    	
    	Log.d("debug", "##############SIZE:"+list.size());
    	return list;
    }
}
